﻿
Partial Class Imagenes_Paises
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            CargarGrillaPaises()
        End If
        
    End Sub

    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        btnNuevo.Enabled = False
        btnAceptar.Enabled = True
        gvPaises.Enabled = False
        btnCancelar.Enabled = True
        Limpiar()
    End Sub
    Private Sub CargarGrillaPaises()
        gvPaises.DataSource = PaisesDB.GetPaises
        gvPaises.DataBind()
    End Sub
    Private Sub Limpiar()
        txtPais.Text = ""
        txtidPais.Text = ""
    End Sub
    Private Sub BotonesInicial()
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        gvPaises.Enabled = True
        gvPaises.SelectedIndex = -1
    End Sub

    Protected Sub gvPaises_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvPaises.PageIndexChanging
        gvPaises.PageIndex = e.NewPageIndex
        CargarGrillaPaises()
    End Sub

    Protected Sub gvPaises_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvPaises.SelectedIndexChanged
        txtidPais.Text = gvPaises.SelectedDataKey.Values(0).ToString()
        txtPais.Text = gvPaises.SelectedRow.Cells(1).Text.ToString()
        btnAceptar.Enabled = True

    End Sub

    Protected Sub btnAceptar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Try
            If btnNuevo.Enabled = False Then 'es un nuevo registro
                PaisesDB.InsertarPais(txtPais.Text)
            Else 'es una actualizacion
                PaisesDB.ActualizarPais(txtidPais.Text.ToString(), txtPais.Text.ToString())
            End If
            CargarGrillaPaises()
            Limpiar()
            BotonesInicial()
        Catch ex As Exception
            Session("Error") = ex.Message.ToString()
            Session("Pagina") = "Paises.aspx"
            Server.Transfer("Errores.aspx")
        End Try

    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        txtPais.Text = ""
        txtidPais.Text = ""
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        gvPaises.Enabled = True
        gvPaises.SelectedIndex = -1
    End Sub

    Protected Sub gvPaises_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvPaises.RowDeleting
        Try
            PaisesDB.EliminarPais(Convert.ToInt32(gvPaises.DataKeys(e.RowIndex).Value.ToString()))
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Paises.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        CargarGrillaPaises()

    End Sub
End Class
